package com.wangxinyuchuan.demo.factory;

import com.wangxinyuchuan.demo.template.AbstractHandler;
import com.wangxinyuchuan.demo.template.AbstractHandler2;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

public class HandlerFactory {

    public static Map<String, AbstractHandler> handlerMap = new HashMap<>();
    public static Map<String, AbstractHandler2> handlerMap2 = new HashMap<>();

    public static AbstractHandler getInvokeStrategy(String nickName) {
        return handlerMap.get(nickName);
    }


    public static AbstractHandler2 getInvokeStrategy2(String nickName) {
        return handlerMap2.get(nickName);
    }

    public static void register(String handlerName, AbstractHandler handler) {
        if (!StringUtils.hasText(handlerName) || Objects.isNull(handler)) {
            throw new IllegalArgumentException("nickName or handler must not null");
        }
        handlerMap.put(handlerName, handler);
    }

    public static void register2(String handlerName, AbstractHandler2 handler) {
        if (!StringUtils.hasText(handlerName) || Objects.isNull(handler)) {
            throw new IllegalArgumentException("nickName or handler must not null");
        }
        handlerMap2.put(handlerName, handler);
    }
}
